<?
include_once "gblinclude.lib";

if ($_POST["process_flag"])
{
	if (process())
	{
		echo '
			<script>
				alert("Change Password Successful");
				window.close();
			</script>';
		exit;
	}
	else
	{
		echo '
			<script>
				alert("Change Password Failed");
				window.close();
			</script>';
		exit;
	}
}

$layoutFactory = new CLayoutFactory();

$header = $layoutFactory->getHeader();
$tail = $layoutFactory->getTail();
$body = createContent();

$js = createJs();

echo $header.$js.$body.$tail;


function createContent()
{
	$content = '';
	$content .= '<form id="form_data" action="#" method="POST">';
		$content .= '<table  border="0" cellspacing="0" cellpadding="0" width="100%">';
			$content .= '<tr height="40">';
				$content .= '<td valign="middle" class="td_content_title">&nbsp;'.CUIControls::GetImage("", "/img/transcontent.gif").'&nbsp;Change Password';
				$content .= '</td>';
			$content .= '</tr>';
			$content .= '<tr>';
				$content .= '<td>';
					$content .= '<table width="100%" border="1"  cellpadding="0" cellpadding="0"  class="edit_table_style" style="margin-top:5px">';
					$content .= '<tr>';
						$content .= '<td class="edit_table_style_td_1" nowrap width="150px">&nbsp;Set New Password:</td>';
						$content .= '<td class="edit_table_style_td_2">&nbsp;'.CUIControls::GetEditPassword("password_set1", "", 200, false, "class=\"text_box_style\"").'</td>';
					$content .= '</tr>';
					$content .= '<tr>';
						$content .= '<td class="edit_table_style_td_1" nowrap>&nbsp;Confirm Password:</td>';
						$content .= '<td class="edit_table_style_td_2">&nbsp;'.CUIControls::GetEditPassword("password_set2", "", 200, false, "class=\"text_box_style\"").'</td>';
					$content .= '</tr>';
					$content .= '</table>';
				$content .= '</td>';
			$content .= '</tr>';
			$content .= '<tr height="35px">';
			$content .= '<td  align="center">&nbsp;';
				$content .= CUIControls::GetHidden("user_id", $_GET["user_id"]);
				$content .= CUIControls::GetHidden("process_flag", 1);
				$content .= CUIControls::GetNormalButton("btn_save", "Submit", "button_style", "onclick=\"submitForm();\"").'&nbsp;';
				$content .= CUIControls::GetNormalButton("btn_cancel", "Cancel", "button_style", "onclick=\"window.close();if (child) child.close();\"");
			$content .= '</td>';
					$content .= '</tr>';
		$content .= '</table>';
	$content .= '</form>';

	return $content;
}

function createJs()
{
	$script = '
		<script>
			function submitForm()
			{
				var frmEle = document.getElementById("form_data");
				var passwordEle1 = document.getElementById("password_set1");
				var passwordEle2 = document.getElementById("password_set2");
				if (passwordEle2.value == passwordEle1.value)
				{
					if ( passwordEle1.value.length == 0)
					{
						if (confirm("Use Empty Password?"))
						{
							frmEle.submit();
						}
						else
						{
							return false;
						}
					}
					frmEle.submit();
				}
				else
				{
					alert("Password do not Match");
					return false;
				}
				
			}
		</script>
		';

	return $script;
}

function process()
{
	global $gblDBConnect;

	$userId = UnregisterID($_POST["user_id"]);

	$sql = "UPDATE tb_user SET login_password='".$_POST["password_set1"]."' WHERE user_id=".$userId;
	try
	{
		$gblDBConnect->execute($sql);
		return true;
	}
	catch(PDOException $ex)
	{
		return false;
	}
}

?>
